package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.OrderBargainUserDao;
import com.bootdo.wx.dao.OrderOrderDao;
import com.bootdo.wx.domain.OrderBargainUserDO;
import com.bootdo.wx.domain.OrderOrderDO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.wx.dao.OrderBargainDao;
import com.bootdo.wx.domain.OrderBargainDO;
import com.bootdo.wx.service.OrderBargainService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class OrderBargainServiceImpl implements OrderBargainService {
	@Autowired
	private OrderBargainDao orderBargainDao;
	@Autowired
	private OrderBargainUserDao orderBargainUserDao;
	@Autowired
	private OrderOrderDao orderOrderDao;
	
	@Override
	public OrderBargainDO get(String id){
		return orderBargainDao.get(id);
	}
	
	@Override
	public List<OrderBargainDO> list(Map<String, Object> map){
		return orderBargainDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orderBargainDao.count(map);
	}
	
	@Override
	public int save(OrderBargainDO orderBargain){
		return orderBargainDao.save(orderBargain);
	}
	
	@Override
	public int update(OrderBargainDO orderBargain){
		return orderBargainDao.update(orderBargain);
	}
	
	@Override
	public int remove(String id){
		return orderBargainDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return orderBargainDao.batchRemove(ids);
	}

	@Override
	public PageInfo<Map<String,Object>> getOrderBargainList(Map<String,Object> paramMap) {
		//更新砍价订单状态
		orderBargainDao.updateBargainList(paramMap);
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> list = orderBargainDao.getOrderBargainList(paramMap);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public Map<String,Object> getOrderBargainById(Map<String,Object> paramMap) {
		String bargainOrderId = paramMap.get("bargainOrderId").toString();
		Map<String,Object> bargainMap = orderBargainDao.getOrderBargainList(paramMap).get(0);
		BigDecimal bargainPrice = new BigDecimal(bargainMap.get("bargainPrice").toString());
		BigDecimal oldPrice = new BigDecimal(bargainMap.get("oldPrice").toString());
		BigDecimal havePrice = new BigDecimal(bargainMap.get("havePrice").toString());
		BigDecimal needPrice = oldPrice.subtract(bargainPrice).subtract(havePrice);
		bargainMap.put("needPrice",needPrice);
		String orderId = bargainMap.get("orderId").toString();
		paramMap.put("orderId",orderId);
		List<Map<String,Object>> userList = orderBargainUserDao.getUserByOrder(paramMap);
		bargainMap.put("userList",userList);
		return bargainMap;
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R helpBargain(Map<String,Object> paramMap) {
		String bargainOrderId = paramMap.get("bargainOrderId").toString();
		String userId = paramMap.get("userId").toString();
		OrderBargainDO orderBargainDO = orderBargainDao.get(bargainOrderId);
		String orderId = orderBargainDO.getOrderId();
		//判断订单是否过期
		Date end_at = orderBargainDO.getEndAt();  //砍价截止时间
		Date nowDate = new Date();
		if (nowDate.after(end_at)) {
			OrderOrderDO orderOrderDO = new OrderOrderDO();
			orderOrderDO.setId(orderId);
			orderOrderDO.setUpdateAt(nowDate);
			orderOrderDO.setStatus(9);
			orderOrderDao.update(orderOrderDO);
			return R.error("该订单已过期");
		}
		//判断会员是否已经砍过价
		paramMap.put("orderId",orderId);
		List<OrderBargainUserDO> orderBargainUserDOList = orderBargainUserDao.list(paramMap);
		if (orderBargainUserDOList.size() != 0) {
			return R.error("您已经为该订单砍过价了");
		}
		paramMap.clear();
		paramMap.put("userId", userId);
		paramMap.put("today", DateUtils.getDayBegin());
		if (orderBargainUserDao.todayList(paramMap).size() != 0) {
			return R.error("您一天只能砍一次价");
		}

		BigDecimal minPrice = orderBargainDO.getMinPrice();
		BigDecimal maxPrice = orderBargainDO.getMaxPrice();
		BigDecimal randomPrice = new BigDecimal(Math.random()).multiply(maxPrice.subtract(minPrice)).add(minPrice).setScale(2,BigDecimal.ROUND_DOWN); //会员砍价金额

		paramMap.clear();
		paramMap.put("bargainOrderId",bargainOrderId);
		Map<String,Object> bargainMap = getOrderBargainById(paramMap);
		BigDecimal bargainPrice = new BigDecimal(bargainMap.get("bargainPrice").toString());
		BigDecimal oldPrice = new BigDecimal(bargainMap.get("oldPrice").toString());
		BigDecimal havePrice = new BigDecimal(bargainMap.get("havePrice").toString());
		BigDecimal needPrice = oldPrice.subtract(bargainPrice).subtract(havePrice);
		OrderBargainUserDO orderBargainUserDO = new OrderBargainUserDO();
		if (randomPrice.compareTo(needPrice) != -1) { //砍价完成
			orderBargainUserDO.setKPrice(needPrice);
			//修改订单状态
			OrderOrderDO orderOrderDO = new OrderOrderDO();
			orderOrderDO.setId(orderId);
			orderOrderDO.setUpdateAt(nowDate);
			orderOrderDO.setStatus(1);
			orderOrderDao.update(orderOrderDO);
		} else {
			orderBargainUserDO.setKPrice(randomPrice);
		}
		orderBargainUserDO.setId(UuidUtil.get32UUID());
		orderBargainUserDO.setDelFlag(0);
		orderBargainUserDO.setUserId(userId);
		orderBargainUserDO.setOrderId(orderId);
		orderBargainUserDO.setCreateAt(nowDate);
		orderBargainUserDao.save(orderBargainUserDO);
		bargainMap.put("minePrice",orderBargainUserDO.getKPrice());
		return R.ok().put("data",bargainMap);
	}
	
}
